package com.server.service;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Vector;

import com.entity.Announcement;
import com.entity.Campus;
import com.entity.Collage;
import com.entity.Department;
import com.entity.Grade;
import com.entity.Major;
import com.entity.Class;
import com.entity.Message;
import com.entity.Result;
import com.entity.Student;
import com.entity.Teacher;
import com.entity.User;
import com.server.dao.impl.LoginDao;
import com.server.dao.impl.RoatDao;
import com.util.Command;

public class ServerThread extends Thread{
	 LoginDao loginDao=new LoginDao();
	 RoatDao roatDao=new RoatDao();
	 private Socket socket;
	private ObjectInputStream is;
	private ObjectOutputStream os;
 public ServerThread(Socket socket) throws IOException{
	 this.socket=socket;
	
 }
 
 @SuppressWarnings("unchecked")
public void run() {
	 Message message;
	
	try {
		while(true){
			if(socket.isClosed()){
				break;
			}
	
	    is=new ObjectInputStream(socket.getInputStream());
		os=new ObjectOutputStream(socket.getOutputStream());
		 
		message=(Message)is.readObject();
		Result result=new Result();
		if(message==null || message.getOpr().equals(Command.EXIT)){
			break;
		}else if(message.getOpr().equals(Command.LOGIN)){
			 User user=(User)message.getObject();
			User reUser=loginDao.query(user);
			result.setObject(reUser);
		 }else if(message.getOpr().equals(Command.CAMPUS_QUERY_ALL)){
			Vector<Vector<Object>> campuses= roatDao.campusesQuerry();
			result.setObject(campuses);
		 }else if(message.getOpr().equals(Command.CAMPUS_QUERY)){
			 Campus campus=roatDao.campusQuerry((String)message.getObject());
			 result.setObject(campus);
		 }else if(message.getOpr().equals(Command.CAMPUS_UPDATE)){
			 int row=roatDao.updateCampus((Campus)message.getObject());
			 result.setState(row);
		 }else if(message.getOpr().equals(Command.CAMPUS_ADD)){
			 int row=roatDao.addCampus((Campus)message.getObject());
			 result.setState(row);
		 }else if(message.getOpr().equals(Command.CAMPUS_DEL)){
			int row=roatDao.delCampus((ArrayList<String>)message.getObject());
			 result.setState(row);
		 }else if(message.getOpr().equals(Command.COLLAGE_QUERY_ALL)){
			 Vector<Vector<Object>> collages=roatDao.queryCollage((String)message.getObject());
			 result.setObject(collages);
		 }else if(message.getOpr().equals(Command.COLLAGE_QUERY)){
			 Collage collage=roatDao.collageQuerry((String)message.getObject());
			 result.setObject(collage);
		 }else if(message.getOpr().equals(Command.COLLAGE_UPDATE)){
			 int row=roatDao.updateCollage((Collage)message.getObject());
			 result.setState(row);
		 }else if(message.getOpr().equals(Command.COLLAGE_ADD)){
			 int row=roatDao.addCollage((Collage)message.getObject());
			 result.setState(row);
		 }else if(message.getOpr().equals(Command.COLLAGE_DEL)){
			int row=roatDao.delCollage((ArrayList<String>)message.getObject());
			 result.setState(row);
		 }else if(message.getOpr().equals(Command.DEPARTMENT_QUERY_ALL)){
			 Vector<Vector<Object>> departments=roatDao.queryDepartments((String)message.getObject());
			 result.setObject(departments);
		 }else if(message.getOpr().equals(Command.DEPARTMENT_QUERY)){
			 Department department=roatDao.departmentQuerry((String)message.getObject());
			 result.setObject(department);
		 }else if(message.getOpr().equals(Command.DEPARTMENT_UPDATE)){
			 int row=roatDao.updateDepartment((Department)message.getObject());
			 result.setState(row);
		 }else if(message.getOpr().equals(Command.DEPARTMENT_ADD)){
			 int row=roatDao.addDepartment((Department)message.getObject());
			 result.setState(row);
		 }else if(message.getOpr().equals(Command.DEPARTMENT_DEL)){
				int row=roatDao.delDepartment((ArrayList<String>)message.getObject());
				 result.setState(row);
			 }else if(message.getOpr().equals(Command.MAJOR_QUERY_ALL)){
				 Vector<Vector<Object>> majors=roatDao.queryMajors((String)message.getObject());
				 result.setObject(majors);
			 }else if(message.getOpr().equals(Command.MAJOR_QUERY)){
				 Major major=roatDao.majorQuery((String)message.getObject());
				 result.setObject(major);
			 }else if(message.getOpr().equals(Command.MAJOR_UPDATE)){
				 int row=roatDao.updateMajor((Major)message.getObject());
				 result.setState(row);
			 }else if(message.getOpr().equals(Command.MAJOR_ADD)){
				 int row=roatDao.addMajor((Major)message.getObject());
				 result.setState(row);
			 }else if(message.getOpr().equals(Command.MAJOR_DEL)){
					int row=roatDao.delMajor((ArrayList<String>)message.getObject());
					 result.setState(row);
				 }else if(message.getOpr().equals(Command.GRADE_QUERY_ALL)){
					 Vector<Vector<Object>> Grades=roatDao.queryGrades();
					 result.setObject(Grades);
				 }else if(message.getOpr().equals(Command.GRADE_QUERY)){
					 Grade grade=roatDao.GradeQuery((String)message.getObject());
					 result.setObject(grade);
				 }else if(message.getOpr().equals(Command.GRADE_UPDATE)){
					 int row=roatDao.updateGrade((Grade)message.getObject());
					 result.setState(row);
				 }else if(message.getOpr().equals(Command.GRADE_ADD)){
					 int row=roatDao.addGrade((Grade)message.getObject());
					 result.setState(row);
				 }else if(message.getOpr().equals(Command.GRADE_DEL)){
						int row=roatDao.delGrade((ArrayList<String>)message.getObject());
						 result.setState(row);
			     }else if(message.getOpr().equals(Command.CLASS_QUERY_ALL)){
					 Vector<Vector<Object>> clas=roatDao.queryClasses((String)message.getObject());
					 result.setObject(clas);
				 }else if(message.getOpr().equals(Command.CLASS_QUERY)){
					 Class cla=roatDao.claQuery((String)message.getObject());
					 result.setObject(cla);
				 }else if(message.getOpr().equals(Command.CLASS_UPDATE)){
					 int row=roatDao.updateClass((Class)message.getObject());
					 result.setState(row);
				 }else if(message.getOpr().equals(Command.CLASS_ADD)){
					 int row=roatDao.addClass((Class)message.getObject());
					 result.setState(row);
				 }else if(message.getOpr().equals(Command.CLASS_DEL)){
						int row=roatDao.delClass((ArrayList<String>)message.getObject());
						result.setState(row);
				 }else if(message.getOpr().equals(Command.ANNOUNCEMENT_QUERY_ALL)){
					 Vector<Vector<Object>> announcements=roatDao.queryAnnouncements();
					 result.setObject(announcements);
				 }else if(message.getOpr().equals(Command.ANNOUNCEMENT_QUERY)){
					 Announcement announcement=roatDao.announcementQuery((String)message.getObject());
					 result.setObject(announcement);
				 }else if(message.getOpr().equals(Command.ANNOUNCEMENT_UPDATE)){
					 int row=roatDao.updateAnnouncement((Announcement)message.getObject());
					 result.setState(row);
				 }else if(message.getOpr().equals(Command.ANNOUNCEMENT_ADD)){
					 int row=roatDao.addAnnouncement((Announcement)message.getObject());
					 result.setState(row);
				 }else if(message.getOpr().equals(Command.ANNOUNCEMENT_DEL)){
						int row=roatDao.delAnnouncements((ArrayList<String>)message.getObject());
						result.setState(row);
				 }else if(message.getOpr().equals(Command.TEACHER_QUERY_ALL)){
					 Vector<Vector<Object>> teachers=roatDao.queryTeachers();
					 result.setObject(teachers);
				 }else if(message.getOpr().equals(Command.TEACHER_QUERY)){
					 Teacher teacher=roatDao.teacherQuery((String)message.getObject());
					 result.setObject(teacher);
				 }else if(message.getOpr().equals(Command.TEACHER_UPDATE)){
					 int row=roatDao.updateTeacher((Teacher)message.getObject());
					 result.setState(row);
				 }else if(message.getOpr().equals(Command.TEACHER_ADD)){
					 int row=roatDao.updateTeacher((Teacher)message.getObject());
					 result.setState(row);
				 }else if(message.getOpr().equals(Command.TEACHER_DEL)){
						int row=roatDao.delTeachers((ArrayList<String>)message.getObject());
						result.setState(row);
				 }else if(message.getOpr().equals(Command.STUDENT_QUERY_ALL)){
					 Vector<Vector<Object>> students=roatDao.queryStudents();
					 result.setObject(students);
				 }else if(message.getOpr().equals(Command.STUDENT_QUERY)){
					 Student student=roatDao.studentQuery((String)message.getObject());
					 result.setObject(student);
				 }else if(message.getOpr().equals(Command.STUDENT_UPDATE)){
					 int row=roatDao.updateStudent((Student)message.getObject());
					 result.setState(row);
				 }else if(message.getOpr().equals(Command.STUDENT_ADD)){
					 int row=roatDao.updateStudent((Student)message.getObject());
					 result.setState(row);
				 }else if(message.getOpr().equals(Command.STUDENT_DEL)){
						int row=roatDao.delStudents((ArrayList<String>)message.getObject());
						result.setState(row);
				 }
		 os.writeObject(result);
		}
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		
	}
	 
} 
}
